package com.gjzt.business.publish.service;

import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.eastdigit.bpm.model.FormEntity;
import com.eastdigit.bpm.service.FormFactory;
import com.eastdigit.bpm.service.IFormService;
import com.eastdigit.bpm.service.ProcessService;
import com.eastdigit.bpm.service.TasksService;
import com.eastdigit.servlet.ReqBean;
import com.eastdigit.system.model.BusinessException;
import com.eastdigit.util.StringUtil;
import com.gjzt.business.customer.model.Customer;
import com.gjzt.business.customer.service.CustomerService;
import com.gjzt.business.publish.inter.PublishBillMapper;
import com.gjzt.business.publish.model.PublishBill;

@Service
public class PublishBillAuditService extends PublishBillService implements IFormService<FormEntity>{
	
	public final static String BEAN_ID = "publishBillAuditService";
	
	 @Autowired
	 private ProcessService processService;
	    
	 @Autowired
	 private TasksService tasksService;
	 
	 @Autowired
	 private CustomerService customerService;
	 
	 @Autowired
	 private PublishBillMapper publishBillMapper;
	 
	/**
	 * 启动
	 * 
	 * @param reqBean
	 */
	public void startPublishBill(ReqBean reqBean) {
		String billId = reqBean.getInfo("billId");
		if (StringUtils.isBlank(billId)) {
			throw new BusinessException("未找到申请单!");
		}
		PublishBill publishBill = publishBillMapper.selectByPrimaryKey(billId);

		Customer customer = customerService.getCusomerInfo(publishBill
				.getCustomerId());
		String title = String.format("[%s]%s", customer.getCustomerName(),
				publishBill.getBillNo());
		processService.startProcessByKey(PublishBill.BILL_AUDIT_PROCESS_KEY,
				title, PublishBillAuditService.BEAN_ID, billId, publishBill
						.toJsonObject(), reqBean.getUserId());
	}
	
	/**
     * 审批待办列表
     * @param reqBean
     * @return
     */
    public JSONObject getTasks(ReqBean reqBean) {
        reqBean.addInfo("processDefinitionKey", PublishBill.BILL_AUDIT_PROCESS_KEY);
        JSONObject result = tasksService.getTasks(reqBean);
        JSONArray tasks = (JSONArray) result.get("rows");
        
        for (Object taskObj : tasks) {
            JSONObject task = (JSONObject) taskObj;
            JSONObject processInstance = task.containsKey("processInstance") ? task.getJSONObject("processInstance")
                    : null;
            if (processInstance != null) {
                String businessKey = processInstance.containsKey("businessKey") ? processInstance
                        .getString("businessKey") : null;
                if (!StringUtil.isEmpty(businessKey)) {
                	String billId = FormFactory.getBusinessId(businessKey);
                	PublishBill bill = publishBillMapper.selectByPrimaryKey(billId);
                	task.put("bill", bill.toJsonObject());
                	task.put("processInstance", processInstance);
                }
            }
        }
        return result;
    }
	
	public FormEntity add(Map<String, String> properties) throws Exception{
		return null;
	}

	public void delete(String id) {
		
	}
	
	public JSONObject toJSONObject(String billId) {
		return publishBillMapper.selectByPrimaryKey(billId).toJsonObject();
	}

	public void update(Map<String, String> properties){
		
	}
}
